package zhuanyeshixi2.basic.global;

/**
 * 全局枚举定义
 * 包含系统中使用的所有枚举类型
 */
public class GlobalEnum {

    /**
     * 错误码枚举 - 定义系统中所有可能的错误类型
     */
    public enum ErrorCode {
        // 成功状态
        SUCCESS("SUCCESS", "操作成功"),

        // 通用错误
        UNKNOWN_ERROR("UNKNOWN_ERROR", "未知错误"),
        INVALID_PARAMETER("INVALID_PARAMETER", "参数验证失败"),
        DATA_NOT_FOUND("DATA_NOT_FOUND", "数据不存在"),
        DATA_ALREADY_EXISTS("DATA_ALREADY_EXISTS", "数据已存在"),
        OPERATION_FAILED("OPERATION_FAILED", "操作失败"),

        // 用户相关错误
        USER_NOT_EXISTS("USER_NOT_EXISTS", "用户不存在"),
        USER_ALREADY_EXISTS("USER_ALREADY_EXISTS", "用户已存在"),
        USER_PASSWORD_ERROR("USER_PASSWORD_ERROR", "密码错误"),
        USER_ACCOUNT_DISABLED("USER_ACCOUNT_DISABLED", "账户已禁用"),
        USER_NOT_LOGIN("USER_NOT_LOGIN", "用户未登录"),

        // 权限相关错误
        UNAUTHORIZED("UNAUTHORIZED", "未授权访问"),
        FORBIDDEN("FORBIDDEN", "禁止访问"),
        PERMISSION_DENIED("PERMISSION_DENIED", "权限不足"),

        // 系统错误
        DATABASE_ERROR("DATABASE_ERROR", "数据库错误"),
        NETWORK_ERROR("NETWORK_ERROR", "网络错误"),
        SYSTEM_BUSY("SYSTEM_BUSY", "系统繁忙"),
        SERVICE_UNAVAILABLE("SERVICE_UNAVAILABLE", "服务不可用"),

        // 文件相关错误
        FILE_UPLOAD_FAILED("FILE_UPLOAD_FAILED", "文件上传失败"),
        FILE_NOT_FOUND("FILE_NOT_FOUND", "文件不存在"),
        FILE_SIZE_EXCEEDED("FILE_SIZE_EXCEEDED", "文件大小超限");

        private final String code;
        private final String message;

        ErrorCode(String code, String message) {
            this.code = code;
            this.message = message;
        }

        public String getCode() {
            return code;
        }

        public String getMessage() {
            return message;
        }

        @Override
        public String toString() {
            return code + ": " + message;
        }

        /**
         * 根据代码获取枚举
         */
        public static ErrorCode fromCode(String code) {
            for (ErrorCode errorCode : values()) {
                if (errorCode.getCode().equals(code)) {
                    return errorCode;
                }
            }
            return UNKNOWN_ERROR;
        }
    }

    /**
     * 用户角色枚举
     */
    public enum UserRole {
        ADMIN("admin", "管理员", 1),
        USER("user", "普通用户", 2);

        private final String code;
        private final String name;
        private final int level;

        UserRole(String code, String name, int level) {
            this.code = code;
            this.name = name;
            this.level = level;
        }

        public String getCode() {
            return code;
        }

        public String getName() {
            return name;
        }

        public int getLevel() {
            return level;
        }

        /**
         * 根据代码获取枚举
         */
        public static UserRole fromCode(String code) {
            for (UserRole role : values()) {
                if (role.getCode().equals(code)) {
                    return role;
                }
            }
            return USER;
        }
    }

    /**
     * 功能类型枚举
     */
    public enum FunctionType {
        MENU(1, "菜单"),
        BUTTON(2, "按钮"),
        API(3, "接口");

        private final int code;
        private final String name;

        FunctionType(int code, String name) {
            this.code = code;
            this.name = name;
        }

        public int getCode() {
            return code;
        }

        public String getName() {
            return name;
        }

        /**
         * 根据代码获取枚举
         */
        public static FunctionType fromCode(int code) {
            for (FunctionType type : values()) {
                if (type.getCode() == code) {
                    return type;
                }
            }
            return MENU;
        }
    }

    /**
     * 操作结果枚举
     */
    public enum OperateResult {
        SUCCESS("SUCCESS", "成功"),
        FAIL("FAIL", "失败");

        private final String code;
        private final String message;

        OperateResult(String code, String message) {
            this.code = code;
            this.message = message;
        }

        public String getCode() {
            return code;
        }

        public String getMessage() {
            return message;
        }

        /**
         * 根据代码获取枚举
         */
        public static OperateResult fromCode(String code) {
            for (OperateResult result : values()) {
                if (result.getCode().equals(code)) {
                    return result;
                }
            }
            return FAIL;
        }
    }

    /**
     * 性别枚举
     */
    public enum Gender {
        MALE(1, "男"),
        FEMALE(2, "女"),
        UNKNOWN(0, "未知");

        private final int code;
        private final String name;

        Gender(int code, String name) {
            this.code = code;
            this.name = name;
        }

        public int getCode() {
            return code;
        }

        public String getName() {
            return name;
        }

        /**
         * 根据代码获取枚举
         */
        public static Gender fromCode(int code) {
            for (Gender gender : values()) {
                if (gender.getCode() == code) {
                    return gender;
                }
            }
            return UNKNOWN;
        }
    }

    /**
     * 用户状态枚举
     */
    public enum UserStatus {
        ACTIVE(1, "活跃"),
        INACTIVE(0, "非活跃"),
        LOCKED(-1, "锁定");

        private final int code;
        private final String name;

        UserStatus(int code, String name) {
            this.code = code;
            this.name = name;
        }

        public int getCode() {
            return code;
        }

        public String getName() {
            return name;
        }

        /**
         * 根据代码获取枚举
         */
        public static UserStatus fromCode(int code) {
            for (UserStatus status : values()) {
                if (status.getCode() == code) {
                    return status;
                }
            }
            return INACTIVE;
        }
    }
}